package com.guli.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.Constant;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.common.utils.Type;
import com.guli.product.dao.AttrAttrgroupRelationDao;
import com.guli.product.dao.AttrGroupDao;
import com.guli.product.dao.CategoryDao;
import com.guli.product.entity.AttrAttrgroupRelationEntity;
import com.guli.product.entity.AttrGroupEntity;
import com.guli.product.entity.CategoryEntity;
import com.guli.product.service.AttrGroupService;
import com.guli.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.product.dao.AttrDao;
import com.guli.product.entity.AttrEntity;
import com.guli.product.service.AttrService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(String type, Long catId, Map<String, Object> params) {
        //根据type获取该属性类型对应的数字
        int attrType = Type.AttrType.getAttrType(type);
        //获取每页索引和显示数目
        long current = Long.parseLong((String) params.get(Constant.PAGE));
        long limit = Long.parseLong((String) params.get(Constant.LIMIT));
        //查询
        Page<AttrEntity> pages = baseMapper.selectPage(new Page<>(current, limit), new QueryWrapper<AttrEntity>()
                .eq(catId != 0, "category_id", catId)
                .eq("attr_type", attrType)
                .like(!StringUtils.isEmpty(params.get(Constant.KEY)), "attr_name", params.get(Constant.KEY)));
        List<AttrEntity> pagesRecords = pages.getRecords();
        if (CollectionUtils.isEmpty(pagesRecords)) {
            return new PageUtils(pages);
        }
        //封装好attrVo
        List<AttrVo> newRecords = pagesRecords.stream().map(attr -> {
            AttrVo attrVo = new AttrVo();
            BeanUtils.copyProperties(attr, attrVo);
            CategoryEntity category = categoryDao
                    .selectOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attr.getCategoryId()));
            if (category != null) {
                attrVo.setCategoryName(category.getName());
                List<AttrAttrgroupRelationEntity> attrgroupRelations = attrAttrgroupRelationDao
                        .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
                if (!CollectionUtils.isEmpty(attrgroupRelations)) {
                    Long attrGroupId = attrgroupRelations.get(0).getAttrGroupId();
                    if (attrGroupId != null) {
                        AttrGroupEntity attrGroup = attrGroupDao
                                .selectOne(new QueryWrapper<AttrGroupEntity>()
                                        .eq("attr_group_id", attrGroupId));
                        attrVo.setGroupName(attrGroup.getAttrGroupName());
                    }
                }
            }
            return attrVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(pages);
        pageUtils.setList(newRecords);
        return pageUtils;
    }

    @Override
    public void saveAndAttrGroup(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, attrEntity);
        baseMapper.insert(attrEntity);
        AttrAttrgroupRelationEntity attrgroupRelation = new AttrAttrgroupRelationEntity();
        attrgroupRelation.setAttrId(attrEntity.getAttrId());
        attrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
        attrAttrgroupRelationDao.insert(attrgroupRelation);
    }

    @Override
    public AttrVo getInfoById(Long attrId) {
        AttrEntity attr = baseMapper.selectById(attrId);
        AttrVo attrVo = new AttrVo();
        BeanUtils.copyProperties(attr, attrVo);
        Long categoryId = attr.getCategoryId();
        CategoryEntity category = categoryDao.selectById(categoryId);
        Long[] categoryPath = attrGroupService.getCategoryPath(categoryId, category);
        attrVo.setCatelogPath(categoryPath);
        List<AttrAttrgroupRelationEntity> attrgroupRelations = attrAttrgroupRelationDao
                .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        if (!CollectionUtils.isEmpty(attrgroupRelations)) {
            Long attrGroupId = attrgroupRelations.get(0).getAttrGroupId();
            if (attrGroupId != null) {
                AttrGroupEntity attrGroup = attrGroupDao.selectById(attrGroupId);
                attrVo.setGroupName(attrGroup.getAttrGroupName());
            }
        }
        return attrVo;
    }

}